package code.example.base.util.concurrent.container;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class TestConcurrentLinkQueue {


    public void test(){
        AtomicInteger id = new AtomicInteger();
        ConcurrentHashMap<Long,Long> longMap = new ConcurrentHashMap<Long,Long>();
        ConcurrentLinkedQueue<Long > nodes = new ConcurrentLinkedQueue<Long>();
        ExecutorService executorService =  Executors.newFixedThreadPool(30);
        IntegerRunnable integerRunnable = new IntegerRunnable(id,longMap,nodes);
        for(int i=0;i<1;i++){
            executorService.execute(integerRunnable);
        }

    }
    class IntegerRunnable implements  Runnable{
        AtomicInteger id = null;
        ConcurrentHashMap<Long,Long> longMap= null;
        ConcurrentLinkedQueue<Long > nodes = null;
        public IntegerRunnable(AtomicInteger id ,ConcurrentHashMap<Long,Long> longMap,  ConcurrentLinkedQueue<Long > nodes) {
            this.id = id;
            this.longMap = longMap;
            this.nodes = nodes;
        }
        @Override
        public void run() {
            AtomicInteger id  = this.id;

            ConcurrentLinkedQueue<Long > nodes = this.nodes;
            while(true){
             /*   try {
                    TimeUnit.SECONDS.sleep(10);
                }catch (Exception   e){
                    e.printStackTrace();
                }*/
                long value = id.incrementAndGet();
                if(value > Integer.MAX_VALUE -10000){
                    System.out.println("finish");
                    return;
                }
                if(value ==25000000){
                    try {
                        TimeUnit.SECONDS.sleep(10000);
                    }catch (Exception e){}

                    System.out.println(value);
                }
                nodes.offer(value);
                Long  pollValue = nodes.poll();
                if(pollValue == null){
                    System.out.println("poll() ==null");
                    return;
                }

            }

        }
    }
    public static void main(String args []){
        new TestConcurrentLinkQueue().test();
    }

}
